package com.bishe.demo.modules.hotel.hotel.service.impl;

import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.hotel.hotel.dao.HotelOrderDao;
import com.bishe.demo.modules.hotel.hotel.dao.HotelOrderHotelMainDao;
import com.bishe.demo.modules.hotel.hotel.entity.HotelOrder;
import com.bishe.demo.modules.hotel.hotel.entity.HotelOrderHotelMain;
import com.bishe.demo.modules.hotel.hotel.service.HotelOrderService;
import com.bishe.demo.modules.hotel.hotelRoom.dao.RoomDao;
import com.bishe.demo.modules.hotel.hotelRoom.entity.Room;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class HotelOrderServiceImpl implements HotelOrderService {
    @Autowired
    private HotelOrderDao hotelOrderDao;

    @Autowired
    private RoomDao roomDao;

    @Autowired
    private HotelOrderHotelMainDao hotelOrderHotelMainDao;

    @Override
    public int countHotelOrder(HotelOrder hotelOrder) {
        return hotelOrderDao.countHotelOrder(hotelOrder);
    }

    @Override
    @Transactional
    public ResultEntity<HotelOrder> insertHotelOrder(HotelOrder hotelOrder) {
        Room roomByRoomId = roomDao.getRoomByRoomId(hotelOrder.getRoomId());
        int i = countHotelOrder(hotelOrder);
        if (i < roomByRoomId.getRoomNum()) {
            hotelOrder.setTime(LocalDateTime.now());
            hotelOrderDao.insertHotelOrder(hotelOrder);
            hotelOrderHotelMainDao.insertHotelOrderHotelMain(new HotelOrderHotelMain(hotelOrder.getOrderId(), hotelOrder.getHotelMain().getHotelMainId()));
            return new ResultEntity<HotelOrder>(ResultEntity.ResultStatus.SUCCESS.code, "下单成功", hotelOrder);
        }
        return new ResultEntity<HotelOrder>(ResultEntity.ResultStatus.FAILED.code, "该段日期房间已满，请选择其他房型或时间段", hotelOrder);
    }

    @Override
    public List<HotelOrder> getHotelOrdersByUserId(int userId) {
        return hotelOrderDao.getHotelOrdersByUserId(userId);
    }
}
